/**
 * 
 */
package org.mips.Sirius.ui;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;

import android.graphics.drawable.Drawable;
import android.util.Log;

public class MarkerFactory {

	private final static String chartsApiPrefix = "http://chart.apis.google.com/chart";
	private static HashMap<String, Drawable> cachedMarkers = new HashMap<String, Drawable>();

	public static Drawable createDefaultMarker() throws SiriusUIException {
		Drawable marker = null;
		String markerKey = "default";
		String chartsApiParameters = "?chst=d_map_spin&chld=0.8|0|FFFF42|15|b|";

		// get cached marker if present
		if (cachedMarkers.containsKey(markerKey)) {
			marker = cachedMarkers.get(markerKey);
		} else {
			marker = createGoogleMarker(chartsApiParameters, markerKey);
		}

		marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker
				.getIntrinsicHeight());

		return marker;
	}

	/*
	 * Create a default marker for a vehicle, with a label
	 */
	public static Drawable createVehicleMarker(String label, String colour)
			throws SiriusUIException {
		Drawable marker = null;

		if (colour == null) {
			colour = "FFFF42";
		}

		String chartsApiParameters = "?chst=d_map_spin&chld=0.8|0|" + colour
				+ "|15|b|" + label;

		// get cached marker if present
		if (cachedMarkers.containsKey(label)) {
			marker = cachedMarkers.get(label);
		} else {
			marker = createGoogleMarker(chartsApiParameters, label);
		}

		marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker
				.getIntrinsicHeight());

		return marker;
	}

	/*
	 * Create a default marker for a stop
	 */
	public static Drawable createStopMarker(String id) throws SiriusUIException {
		Drawable marker = null;

		String chartsApiParameters = "?chst=d_map_pin_icon&chld=bus|FFFFFF";

		// get cached marker if present
		if (cachedMarkers.containsKey(id)) {
			marker = cachedMarkers.get(id);
		} else {
			marker = createGoogleMarker(chartsApiParameters, id);
		}

		marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker
				.getIntrinsicHeight());

		return marker;
	}

	/*
	 * Creates a marker bitmap with Google's Chart API
	 */
	private static Drawable createGoogleMarker(String chartsApiParameters,
			String label) throws SiriusUIException {
		Drawable marker = null;

		// FIXME: save background "pin" image and write text over it
		try {
			Log.d("debug", "Calling Google for a marker: " + label);
			URL markerBitmapURL = new URL(chartsApiPrefix + chartsApiParameters);
			HttpURLConnection connection = (HttpURLConnection) markerBitmapURL
					.openConnection();
			connection.setDoInput(true);
			connection.connect();
			InputStream input = connection.getInputStream();
			marker = Drawable.createFromStream(input, "foo");
			cachedMarkers.put(label, marker);
		} catch (IOException e) {
			Log.d("debug", "Seems like we're not connected to the Internet");
			throw new SiriusUIException(e,
					"Seems like we're not connected to the Internet");
		} catch (Exception e) {
			Log.d("debug",
					"MarkerFactory::createStopMarker: Couldn't create marker for label: "
							+ label);
		}

		return marker;
	}
}
